﻿<?php

//////////////////////////////////////////////////////////////////////////
//
// WikiHiero2 - A PHP convert from text using "Manual for the encoding of
// hieroglyphic texts for computer input" syntax to HTML entities (table and
// images).
//
// Copyright (C) 2004-2007 Guillaume Blanchard (Aoineko)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//////////////////////////////////////////////////////////////////////////

require_once("wh_tools.php");

////////////////////////////////////////////////////////////////////////////////
/**
 *
 */
class WH_Variable
{
	const Boolean = 0;
	const Integer = 2;
	const String  = 3;
	const Enum    = 4;
	
	protected $name;
	protected $type;
	protected $value;
	protected $enum;

	public function __construct($name, $type, $value = null, $enum = null)
	{
		$this->name = $name;
		$this->type = $type;
		$this->enum = $enum;
		if($value !== null)
		{
			$this->SetValue($value);
		}
		else
		{
			switch($this->type)
			{
				case Boolean: $this->value = false; break;
				case Integer: $this->value = 0; break;
				case String:  $this->value = ""; break;
				case Enum:
					if(($enum == null) || (count($enum) == 0))
						WH_Error("Invalid enum: $enum");
					$this->value = reset($enum); 
					break;
				default: WH_Error("Unvalid variable type: $type");
			}
		}
	}

	public function &GetName()  { return $this->name; }
	public function &GetType()  { return $this->type; }
	public function &GetValue() { return $this->value; }
	public function &GetEnum()  { return $this->enum; }
	public function SetValue($value)
	{
		if($this->type == WH_Variable::Enum && (($this->enum == null) || !in_array($value, $this->enum)))
		{
			WH_Error("Try to set invalid enum value: $value");
		}
		else if($this->type == WH_Variable::Boolean)
		{
			if($value === "on") // support HTML form boolean value: true = "on", false = ""
				$value = 1;
		}
		$this->value = $value;
	}

}

////////////////////////////////////////////////////////////////////////////////
/**
 *
 */
abstract class WH_Plugin
{
	const TypeDefault  = 0;
	const TypeImporter = 1;
	const TypeModifier = 2;
	const TypeExporter = 3;

	protected $variables = array();

	public static function CanImport() { return false; }
	public static function CanModify() { return false; }
	public static function CanExport() { return false; }
	public static function CanDisplay() { return false; } // use for expoter to display result (text, image, etc.)

	public function &GetVariables() { return $this->variables; }
	public function RegisterVariable($name, $type, $default = null, $enum = null) { $this->variables[$name] = new WH_Variable($name, $type, $default, $enum); }
	public function SetVariable($name, $value) { $this->variables[$name]->SetValue($value); }
	public function &GetVariable($name) { return $this->variables[$name]->GetValue(); }

	public function &Import(&$input) {}    // in: user input; out: HW_Document
	public function &Modify(&$document) {} // in: HW_Document; out: HW_Document
	public function &Export(&$document) {} // in: HW_Document; out: user output
	public function Display(&$output) {}   // in: user output

	// Abstract member functions: must be implemented by derivated class.

	public static function GetId() {} // Plugin id (string[8]])
	public static function GetName() {} // Plugin name (string[32]])
	public static function GetDesc($type = WH_Plugin::TypeDefault, $lang = "") {}
	public static function GetVersion() {} // Plugin version (ver.maj.med.min)
	public static function GetCredit($lang = "") {} // Plugin credit
}

?>